package com.ibeeking.found.upms.s.rest.service.impl;

import com.ibeeking.found.common.enums.PublishStatusEnum;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.upms.s.rest.service.IShopUserRelationService;
import com.ibeeking.found.upms.service.common.bo.ShopUserRelationBO;
import com.ibeeking.found.upms.service.common.dos.ShopUserRelationDO;
import com.ibeeking.found.upms.service.common.query.ShopUserPageQuery;
import com.ibeeking.found.upms.service.mapper.ShopUserRelationMapper;
import com.ibeeking.nematos.redis.utils.RedisUtils;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName ShopUserRelationServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-04-26 10:48
 **/
@Service
public class ShopUserRelationServiceImpl implements IShopUserRelationService {
    private static final String SHOP_SHOP_USER_KEY_LOCK = "SHOP_SHOP_USER_KEY_LOCK:";

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ShopUserRelationMapper shopUserRelationMapper;

    @Override
    public Boolean add(Long shopId, Long shopUserId) {
        ShopUserRelationDO add = new ShopUserRelationDO();
        add.setId(shopUserId);
        add.setShopId(shopId);
        add.setJob(PublishStatusEnum.ENABLE.getValue());
        add.setLock(PublishStatusEnum.ENABLE.getValue());
        if (redisUtils.tryLock(SHOP_SHOP_USER_KEY_LOCK + shopUserId)) {
            try {
                //校验是否当前租户下存在此账号
                ShopUserRelationDO shopUserRelationDO = shopUserRelationMapper.selectOne(new LambdaQueryWrapper<>(ShopUserRelationDO.class).eq(ShopUserRelationDO::getId, shopUserId).eq(ShopUserRelationDO::getShopId, shopId));
                if (null == shopUserRelationDO) {
                    add.setCreateBy(UserUtils.getUserId());
                    add.setCreateTime(LocalDateTime.now());
                    add.setModifyTime(LocalDateTime.now());
                    shopUserRelationMapper.insert(add);
                } else {
                    add.setModifyBy(UserUtils.getUserId());
                    add.setModifyTime(LocalDateTime.now());
                    shopUserRelationMapper.update(add, new LambdaUpdateWrapper<>(ShopUserRelationDO.class).eq(ShopUserRelationDO::getId, shopUserId).eq(ShopUserRelationDO::getShopId, shopId));
                }
            } finally {
                redisUtils.unlock(SHOP_SHOP_USER_KEY_LOCK + shopUserId);
            }
            return true;
        }
        return false;
    }

    @Override
    public Boolean lockUser(Long shopId, Long shopUserId, Integer status) {
        ShopUserRelationDO add = new ShopUserRelationDO();
        add.setShopId(shopId);
        add.setId(shopUserId);
        add.setLock(status);
        add.setModifyBy(UserUtils.getUserId());
        add.setModifyTime(LocalDateTime.now());
        return shopUserRelationMapper.update(add, new LambdaUpdateWrapper<>(ShopUserRelationDO.class).eq(ShopUserRelationDO::getId, shopUserId).eq(ShopUserRelationDO::getShopId, shopId)) > 0;
    }

    @Override
    public Boolean quitById(Long shopId, Long shopUserId) {
        ShopUserRelationDO add = new ShopUserRelationDO();
        add.setShopId(shopId);
        add.setId(shopUserId);
        add.setJob(PublishStatusEnum.DISABLE.getValue());
        add.setLock(PublishStatusEnum.DISABLE.getValue());
        add.setModifyBy(UserUtils.getUserId());
        add.setModifyTime(LocalDateTime.now());
        return shopUserRelationMapper.update(add, new LambdaUpdateWrapper<>(ShopUserRelationDO.class).eq(ShopUserRelationDO::getId, shopUserId).eq(ShopUserRelationDO::getShopId, shopId)) > 0;
    }

    @Override
    public List<ShopUserRelationDO> selectByCondition(Long shopId, ShopUserPageQuery query) {
        LambdaQueryWrapper<ShopUserRelationDO> queryWrapper = new LambdaQueryWrapper<>(ShopUserRelationDO.class)
                .orderByDesc(ShopUserRelationDO::getCreateTime)
                .eq(ShopUserRelationDO::getShopId, shopId)
                .eq(null != query.getJob(), ShopUserRelationDO::getJob, query.getJob())
                .eq(null != query.getPublishStatus(), ShopUserRelationDO::getLock, query.getPublishStatus());
        List<ShopUserRelationDO> shopUserRelationDos = shopUserRelationMapper.selectList(queryWrapper);
        if (Collections3.isEmpty(shopUserRelationDos)) {
            return Collections3.emptyList();
        }
        return shopUserRelationDos;
    }

    @Override
    public List<ShopUserRelationBO> queryShopByUserIds(List<Long> userIds) {
        List<ShopUserRelationDO> shopUserRelationDos = shopUserRelationMapper.selectList(new LambdaQueryWrapper<>(ShopUserRelationDO.class)
                .in(ShopUserRelationDO::getId, userIds)
        );
        return JsonUtils.jsonToList(shopUserRelationDos, ShopUserRelationBO.class);
    }

    @Override
    public Boolean delete(Long shopId, Long userId) {
        return shopUserRelationMapper.delete(new LambdaUpdateWrapper<>(ShopUserRelationDO.class)
                .eq(ShopUserRelationDO::getShopId, shopId)
                .eq(ShopUserRelationDO::getId, userId)
        ) > 0;
    }

    @Override
    public ShopUserRelationBO selectByUserId(Long shopId, Long userId) {
        ShopUserRelationDO shopUserRelationDo = shopUserRelationMapper.selectOne(new LambdaQueryWrapper<>(ShopUserRelationDO.class)
                .eq(ShopUserRelationDO::getShopId, shopId)
                .eq(ShopUserRelationDO::getId, userId)
        );
        return BeanUtil.convertBean(shopUserRelationDo, ShopUserRelationBO.class);
    }
}
